<?php
namespace App\Admin\Controllers\SysAccount;

use App\Admin\Actions\Tree\SyncSysAccountMenu;
use App\Admin\Controllers\BaseAdminController;
use App\Models\Open\SysAccount;
use Dcat\Admin\Form;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Tree;
use Dcat\Admin\Widgets\Box;
use Dcat\Admin\Widgets\Form as WidgetForm;

class SysAccountMenuController extends BaseAdminController
{
    private $sysAccount;

    public function title()
    {
        if($this->sysAccount) {
            return trans('sys-account-menu.labels.sys-account-menu').'['.$this->sysAccount->name.']';
        }
        return trans('sys-account-menu.labels.sys-account-menu');
    }

    public function index(Content $content)
    {
        $said = request('said');
        if(!$said) return redirect(admin_url('sys-account'));
        $sysAccount = SysAccount::find($said);
        if(!$sysAccount) return redirect(admin_url('sys-account'));
        $this->sysAccount = $sysAccount;
        $content->view('admin.layouts.content');
        return $content
            ->title($this->title())
            ->body(function (Row $row) use ($said) {
                $row->column(7, $this->treeView()->render());

                $row->column(5, function (Column $column) use ($said) {
                    $form = new WidgetForm();
                    $form->action(admin_url('sys-account-menu').'?said='.$said);

                    $form->hidden('sys_account_id')->value($said);
                    $form->select('parent_id', '父级')->options(SysAccountMenu::selectOptions());
                    $form->text('title', '标题')->required();
                    $form->select('type', '类型')->options(trans('sys-account-menu.options.types'))
                        ->when('view', function (WidgetForm $form) {
                            $form->text('url', 'URL');
                        })
                        ->when('miniprogram', function (WidgetForm $form) {
                            $form->text('appid', 'APPID');
                            $form->text('pagepath', '小程序路径');
                            $form->text('bakurl', '备用URL');
                        })
                        ->when('click', function (WidgetForm $form) {
                            $form->text('key', '关键字');
                        });

                    $form->width(9, 2);

                    $column->append(Box::make('新增', $form));
                });
            });
    }

    protected function treeView()
    {
        return new Tree(SysAccountMenu::class, function (Tree $tree) {
            $tree->disableCreateButton();
            $tree->disableQuickCreateButton();
            $tree->disableEditButton();
            $tree->maxDepth(2);

            $tree->actions(function (Tree\Actions $actions) {
                if ($actions->getRow()->extension) {
                    $actions->disableDelete();
                }

                $actions->prepend(new \Dcat\Admin\Http\Actions\Menu\Show());
            });

            $tree->branch(function ($branch) {
                if (! isset($branch['children'])) {
                    $types = trans('sys-account-menu.options.types');
                    $type = @$branch['type'] ? '[' . $types[$branch['type']] . ']' : '';
                    $payload = "<i class='fa {$branch['icon']}'></i>&nbsp;<strong>{$branch['title']}{$type}</strong>";
                    $url = '';
                    if($branch['type'] == 'view') {
                        $url = $branch['url'];
                    } else if($branch['type'] == 'miniprogram') {
                        $url = $branch['pagepath'];
                    }
                    $payload .= "&nbsp;&nbsp;&nbsp;<a href=\"$url\" class=\"dd-nodrag\">$url</a>";
                } else {
                    $payload = "<i class='fa {$branch['icon']}'></i>&nbsp;<strong>{$branch['title']}</strong>";
                }

                return $payload;
            });

            $tree->tools(function (Tree\Tools $tools) {
                $said = request('said');
                $tools->add(SyncSysAccountMenu::make()->setKey($said));
            });
        });
    }

    public function form()
    {
        return Form::make(SysAccountMenu::class, function (Form $form) {
            $form->tools(function (Form\Tools $tools) {
                $tools->disableView();
            });

            $form->display('id', 'ID');

            $form->hidden('sys_account_id');
            $form->select('parent_id', '父级')->options(SysAccountMenu::selectOptions());
            $form->text('title', '标题')->required();
            $form->select('type', '类型')->options(trans('sys-account-menu.options.types'))
                ->when('view', function (Form $form) {
                    $form->text('url', 'URL');
                })
                ->when('miniprogram', function (Form $form) {
                    $form->text('appid', 'APPID');
                    $form->text('pagepath', '小程序路径');
                    $form->text('bakurl', '备用URL');
                })
                ->when('click', function (Form $form) {
                    $form->text('key', '关键字');
                });
//        })->saved(function (Form $form, $result) {
//            $response = $form->response()->location('auth/menu');
//
//            if ($result) {
//                return $response->success(__('admin.save_succeeded'));
//            }
//
//            return $response->info(__('admin.nothing_updated'));
        });
    }
}
